package com.example.demo.leetcode.classics150;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _143最小路径和
 * @description
 * @date 2023/08/27 11:20
 * <p>
 * ******************************************************
 */
public class _143最小路径和 {

    public int minPathSum(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int[][] dp = new int[m][n];
        dp[0][0] = grid[0][0];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (j - 1 < 0 && i - 1 < 0) {
                    continue;
                } else if (j - 1 < 0) {
                    dp[i][j] = grid[i][j] + dp[i - 1][j];
                } else if (i - 1 < 0) {
                    dp[i][j] = grid[i][j] + dp[i][j - 1];
                } else {
                    dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]);
                }

            }
        }
        return dp[m - 1][n - 1];
    }
}
